package com.tangkun.environment.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.tangkun.environment.R;
import com.tangkun.environment.activity.JuBaoYiChangDetailActivity;
import com.tangkun.environment.entity.gongzuo.ReportIndexReturn;
import com.tangkun.environment.network.GlobalUrl;
import com.tangkun.environment.utils.Utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author tangkun
 * @ClassName: YiChangShuJuTiJiaoAdapter
 * @Description: 异常数据提交列表适配器
 * @date 2016-08-20 上午11:57:36
 */
public class YiChangShuJuTiJiaoAdapter extends BaseAdapter {

    private Context context;
    private List<ReportIndexReturn.DataBean.ListBean> dataList = new ArrayList<ReportIndexReturn.DataBean.ListBean>();

    public YiChangShuJuTiJiaoAdapter(Context context, List<ReportIndexReturn.DataBean.ListBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_yichangshujutijiao, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (dataList != null) {
            /*** 轮播图设置 ***/
            if (!TextUtils.isEmpty(dataList.get(position).reportimgs)) {
                //轮播间隔时间
                holder.rollPagerView.setPlayDelay(30000 * 1000);
                //动画时间
                holder.rollPagerView.setAnimationDurtion(500);
                //设置指示器颜色
                holder.rollPagerView.setHintView(new ColorPointHintView(context, context.getResources().getColor(R.color.common_green), context.getResources().getColor(R.color.common_bg)));
                //刷新广播轮播图
                String reportimgs = dataList.get(position).reportimgs;
                String[] reportimgsArray = reportimgs.split(",");
                List<String> urlList = Arrays.asList(reportimgsArray);
                RollviewpagerAdapter rollviewpagerAdapter = new RollviewpagerAdapter(urlList, position);
                holder.rollPagerView.setAdapter(rollviewpagerAdapter);
                holder.rollPagerView.setVisibility(View.VISIBLE);
            } else {
                holder.rollPagerView.setVisibility(View.INVISIBLE);
            }

            //状态 unhandle，待处理；handle，已处理；uncheck、待巡查；check、已巡查；unfile，未归档；file、已归档
            switch (dataList.get(position).statuscode) {
                case "unhandle"://待处理
                    holder.tvYichangshujutijiaoZhuangtai.setText("待处理");
                    holder.tvYichangshujutijiaoZhuangtai.setBackgroundResource(R.drawable.shape_rectangle_red_solid);
                    break;
                case "handle"://已处理
                    holder.tvYichangshujutijiaoZhuangtai.setText("已处理");
                    holder.tvYichangshujutijiaoZhuangtai.setBackgroundResource(R.drawable.shape_rectangle_green_solid);
                    break;
                case "uncheck"://待巡查
                    holder.tvYichangshujutijiaoZhuangtai.setText("待巡查");
                    holder.tvYichangshujutijiaoZhuangtai.setBackgroundResource(R.drawable.shape_rectangle_orange_solid);
                    break;
                case "check"://已巡查
                    holder.tvYichangshujutijiaoZhuangtai.setText("已巡查");
                    holder.tvYichangshujutijiaoZhuangtai.setBackgroundResource(R.drawable.shape_rectangle_green_solid);
                    break;
                case "unfile"://未归档
                    holder.tvYichangshujutijiaoZhuangtai.setText("未归档");
                    holder.tvYichangshujutijiaoZhuangtai.setBackgroundResource(R.drawable.shape_rectangle_green_solid);
                    break;
                case "file"://已归档
                    holder.tvYichangshujutijiaoZhuangtai.setText("已归档");
                    holder.tvYichangshujutijiaoZhuangtai.setBackgroundResource(R.drawable.shape_rectangle_green_solid);
                    break;
                default://默认情况
                    holder.tvYichangshujutijiaoZhuangtai.setText(dataList.get(position).statuscode + "");
                    holder.tvYichangshujutijiaoZhuangtai.setBackgroundResource(R.drawable.shape_rectangle_red_solid);
                    break;
            }
            //名称
            if (!TextUtils.isEmpty(dataList.get(position).enterprisename)) {
                holder.tvYichangshujutijiaoName.setText(dataList.get(position).enterprisename);
            } else {
                holder.tvYichangshujutijiaoName.setText("");
            }
            //时间
            if (!TextUtils.isEmpty(dataList.get(position).createtime)) {
                holder.tvYichangshujutijiaoShijian.setText(Utils.getDateYearMonthDay(dataList.get(position).createtime));
            } else {
                holder.tvYichangshujutijiaoShijian.setText("");
            }

            //查看异常详情
            convertView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent i = new Intent(v.getContext(), JuBaoYiChangDetailActivity.class);
                    i.putExtra("id", dataList.get(position).ids);
                    v.getContext().startActivity(i);
                }
            });
        }
        return convertView;

    }

    /**
     * 智能预警首页轮播图片的适配器
     */
    private class RollviewpagerAdapter extends StaticPagerAdapter {

        private List<String> urlList = new ArrayList<String>();
        private int index;

        public RollviewpagerAdapter(List<String> urlList, int index) {
            this.urlList = urlList;
            this.index = index;
        }

        @Override
        public View getView(ViewGroup container, final int position) {
            ImageView imageView = new ImageView(container.getContext());
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            //轮播图网络地址
            String imageUrl = urlList.get(position);
            Glide.with(context)
                    .load(GlobalUrl.IMG_URL + imageUrl)
                    .placeholder(R.mipmap.ic_banner_error)
                    .centerCrop()
                    .dontAnimate()
                    .into(imageView);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //轮播图点击事件
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(v.getContext(), JuBaoYiChangDetailActivity.class);
                    i.putExtra("id", dataList.get(index).ids);
                    v.getContext().startActivity(i);
                }
            });
            return imageView;
        }


        @Override
        public int getCount() {
            return urlList.size();
        }
    }

    /**
     * 自定义接口回调
     */
    private MyClickListener myClickListener;

    public void setMyClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public interface MyClickListener {
        void onMyClick(int position);
    }

    static class ViewHolder {
        @BindView(R.id.rollviewpager)
        RollPagerView rollPagerView;
        @BindView(R.id.iv_yichangshujutijiao_img)
        ImageView ivYichangshujutijiaoImg;
        @BindView(R.id.tv_yichangshujutijiao_zhuangtai)
        TextView tvYichangshujutijiaoZhuangtai;
        @BindView(R.id.tv_yichangshujutijiao_name)
        TextView tvYichangshujutijiaoName;
        @BindView(R.id.tv_yichangshujutijiao_shijian)
        TextView tvYichangshujutijiaoShijian;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
